//
//  JoinSureViewController.m
//  LoverGo
//
//  Created by liudonghuan on 14-9-11.
//  Copyright (c) 2014年 yexiao. All rights reserved.
//

#import "JoinSureViewController.h"
#import "UserTableViewCell.h"
#import "PayViewController.h"
#import "LGUtil.h"
#import "Toast+UIView.h"
@interface JoinSureViewController ()

@end

@implementation JoinSureViewController
//
-(id)initWithDic:(NSMutableDictionary *)dic Time:(NSString *)time PayType:(PayType)type
{
    self = [super init];
    if (self) {
        _joinDict = dic;
        _activityTime = time;
        _paytype = type;
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

-(void)loadView
{
    [self dinglan];
    _joinSureView = [[JoinSureView alloc]init];
    _joinSureView.jDelegate = self;
    switch (_paytype) {
        case 0://来自活动下单
            _joinSureView.pay.hidden = NO;
            _joinSureView.deletepay.hidden = YES;
            break;
        case 1://来自未付款
            _joinSureView.pay.hidden = NO;
            _joinSureView.deletepay.hidden = NO;

            break;
        case 2://来自已付款
            _joinSureView.pay.hidden = YES;
            _joinSureView.deletepay.hidden = YES;
            break;
            
        default:
            break;
    }
    _joinSureView.titleLab.text = [NSString stringWithFormat:@"%@",[_joinDict objectForKey:@"orderName"]];
    _joinSureView.priceLab.text = [NSString stringWithFormat:@"%@",[_joinDict objectForKey:@"totalPrice"]];
    _joinSureView.totalPriceLab.text = [NSString stringWithFormat:@"%@",[_joinDict objectForKey:@"totalPrice"]];
    _joinSureView.timeLab.text = _activityTime;
    _joinSureView.addTimeLab.text = [_joinDict objectForKey:@"insertTime"];
    
    [self setView:_joinSureView];
}
-(void)dinglan{
    UIButton * backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,20, 20)];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(dinglanBackBtn) forControlEvents:UIControlEventTouchUpInside] ;
    [backBtn setShowsTouchWhenHighlighted:YES] ;
    UIBarButtonItem *backbutton = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backbutton;
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(90, 0, 140, 30)];
    title.textAlignment = UITextAlignmentCenter;
    title.textColor = LGCOLOR_blue;
    title.text = @"订单确认";
    self.navigationItem.titleView = title;
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"barBG.png"] forBarMetrics:UIBarMetricsDefault];
}
-(void)dinglanBackBtn{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)deletePay
{
    UIAlertView *alertfirst = [[UIAlertView alloc]initWithTitle:@"删除订单" message:@"是否放弃该订单？" delegate:self cancelButtonTitle:@"我不买了" otherButtonTitles:@"我点错了", nil] ;
    [alertfirst show];
}
//处理弹出框事件
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [self doDeletePay];
        }
}
-(void)doDeletePay
{
    NSDictionary *para = [NSDictionary dictionaryWithObjectsAndKeys:
                          [_joinDict objectForKey:@"uuid"],@"activityOrderUuid",
                          nil];
    [SVHTTPRequest POST:@"http://server.lover-go.com/activityOrder/deleteActivityOrder"
             parameters:para
             completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {
                 if (error) {
                     [[[[UIApplication sharedApplication] delegate] window] makeToast:@"网络接连失败"
                                                                             duration:1.0
                                                                             position:@"center"];
                     return ;
                 }
                 NSError *jsonError = nil;
                 id jsonObject = [NSJSONSerialization JSONObjectWithData:response options:kNilOptions error:&jsonError];
                 if (jsonError) {
                     [[[[UIApplication sharedApplication] delegate] window] makeToast:@"数据解析异常"
                                                                             duration:1.0
                                                                             position:@"center"];
                 }else{
                     if ([[jsonObject objectForKey:@"status"] intValue] == 0) {
                         [[[[UIApplication sharedApplication] delegate] window] makeToast:@"删除订单成功"
                                                                                 duration:1.0
                                                                                 position:@"center"];
                         [self.navigationController popViewControllerAnimated:YES];
                     }
                     
                 }
                 
             }];
}
-(void)surePay
{
    self.navigationItem.backBarButtonItem = [LGUtil getBackButton];
    PayViewController *payV = [[PayViewController alloc]initWithOrderUuid:[_joinDict objectForKey:@"uuid"]];
    [self.navigationController pushViewController:payV animated:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"支付确认"];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"支付确认"];
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
